package com.hz.pay.center.service.impl;

import com.hz.pay.center.constant.RediskeyConstant;
import com.hz.pay.center.mapper.MerchantInfoMapper;
import com.hz.pay.center.model.MerchantInfo;
import com.hz.pay.center.service.IMchInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

/**
 * 商户信息相关接口实现类
 */
@Service
public class MchInfoServiceImpl implements IMchInfoService {

    @Autowired
    private MerchantInfoMapper merchantInfoMapper;

    @Autowired
    private ValueOperations<String,Object> valueOperations;


    @Override
        public MerchantInfo queryByMchId(String mchId) {

        //商户配置信息可以放到redis

        MerchantInfo merchantInfo = (MerchantInfo)valueOperations.get(RediskeyConstant.PAY_MCH_MCHID + mchId);

        if (!ObjectUtils.isEmpty(merchantInfo)){
            return merchantInfo ;
        }

         merchantInfo =  merchantInfoMapper.queryMchInfoByMchId(mchId);

        if (!ObjectUtils.isEmpty(merchantInfo)){
            valueOperations.set(RediskeyConstant.PAY_MCH_MCHID + mchId,merchantInfo,2l,TimeUnit.HOURS);
        }

        return merchantInfo;
    }
}
